import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cloudera.api.model.ApiRole;
import com.cloudera.api.model.ApiRoleList;
import com.cloudera.api.v3.AllHostsResource;
import com.cloudera.api.v31.RolesResourceV31;
import com.cloudera.api.v33.RootResourceV33;
import com.cloudera.api.v33.ServicesResourceV33;
import com.infore.common.model.dto.TimeSeriesDto;
import com.infore.exchange.main.utils.ClouderaManagerUtils;
import org.junit.Test;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

public class CmTest02 {

    public static void main(String[] args) {
//        String query = "SELECT total_read_bytes_rate_across_datanodes WHERE entityName = \"hdfs\" AND category = SERVICE";
//        String query = "SELECT physical_memory_used WHERE category= HOST";
//        String query = "select dfs_capacity, dfs_capacity_used, dfs_capacity_used_non_hdfs where entityName=hdfs";
        String query = "select swap_used, physical_memory_used, physical_memory_total, physical_memory_cached, physical_memory_buffers where entityName=738b5d2c-678a-49a5-82f2-b63c5034b289";
//        String from = "2023-05-11 14:20:00";
//        String end = "2023-05-11 14:45:00";
        Date toDate = new Date();
        Date fromDate = DateUtil.offsetMinute(toDate, -10);
        try {
            List<TimeSeriesDto> list = ClouderaManagerUtils.getTimeSeriesResponse(query, fromDate, toDate);
            System.out.println(JSONUtil.toJsonStr(list));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
    public void testDfs() throws Exception{
        Date toDate = new Date();
        Date fromDate = DateUtil.offsetMinute(toDate, -10);
        String query = "select dfs_capacity, dfs_capacity_used, dfs_capacity_used_non_hdfs where entityName=hdfs";
        List<TimeSeriesDto> list = ClouderaManagerUtils.getTimeSeriesResponse(query, fromDate, toDate);
        System.out.println(JSONUtil.toJsonStr(list));
    }
    @Test
    public void testMemory() throws Exception{
        Date toDate = new Date();
        Date fromDate = DateUtil.offsetMinute(toDate, -10);
        String query = "select swap_used, physical_memory_used, physical_memory_total, physical_memory_cached, physical_memory_buffers where entityName=738b5d2c-678a-49a5-82f2-b63c5034b289";
        List<TimeSeriesDto> list = ClouderaManagerUtils.getTimeSeriesResponse(query, fromDate, toDate);
        System.out.println(JSONUtil.toJsonStr(list));
    }

    @Test
    public void testRolesResource() throws Exception{
        RootResourceV33 rootResource = ClouderaManagerUtils.getRootResource();
        ServicesResourceV33 servicesResource = rootResource.getClustersResource().getServicesResource("Cluster");
        RolesResourceV31 rolesResource = servicesResource.getRolesResource("hbase");
        ApiRoleList apiRoles = rolesResource.readRoles();
        List<ApiRole> roles = apiRoles.getRoles();
        for (ApiRole role: apiRoles.getRoles()) {
            System.out.println(JSONUtil.toJsonStr(role));
        }

    }

}
